package meterMonitor.controller;

import meterMonitor.biz.ProductivityBiz;
import meterMonitor.consts.ResponseCode;
import meterMonitor.framework.auth.AuthorityType;
import meterMonitor.framework.auth.FireAuthority;
import meterMonitor.model.Productivity;
import meterMonitor.vo.BaseResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.sql.Timestamp;
import java.util.List;

@RequestMapping("capacity")
@RestController
public class CapacityController {
    public static final Logger logger = LoggerFactory.getLogger(CapacityController.class);
    @Autowired
    ProductivityBiz productivityBiz;

    /**
     * 产能展示页面
     *
     * @param mv
     * @return
     */
    @RequestMapping("capacity.do")
    public ModelAndView index(ModelAndView mv) {
        return mv;
    }

    /**
     * @return 产能信息
     */
    @RequestMapping("getAll.do")
    public List<Productivity> getAll(String fixedDate) {
        Productivity productivity = new Productivity();
        productivity.setFixedDate(fixedDate);
        return productivityBiz.query(productivity);
    }

    /**
     * 新增产能
     *
     * @return
     */
    @RequestMapping("edit.do")
    @FireAuthority(authorityTypes = AuthorityType.Edit)
    @ResponseBody
    public BaseResponseData edit(Productivity productivity) {
        BaseResponseData baseResponseData = new BaseResponseData();
        try {
            if (null == productivity.getPkid()) {
                productivity.setAddedTime(new Timestamp(System.currentTimeMillis()));
                productivityBiz.add(productivity);
            } else {
                productivity.setModifyTime(new Timestamp(System.currentTimeMillis()));
                productivityBiz.update(productivity);
            }
            baseResponseData.setCode(ResponseCode.SUCCESS);
            baseResponseData.setMessage("添加成功！");
        } catch (Exception e) {
            logger.error("新增产能信息异常！", e);
            baseResponseData.setCode(ResponseCode.DATA_EXCEPTION);
            baseResponseData.setMessage("新增产能信息失败！");
        }
        return baseResponseData;
    }

    /**
     * 批量更新产能信息
     *
     * @return
     */
    @RequestMapping("batchEdit.do")
    @FireAuthority(authorityTypes = AuthorityType.Edit)
    @ResponseBody
    public BaseResponseData batchEdit(@RequestBody List<Productivity> productivitys) {
        BaseResponseData baseResponseData = new BaseResponseData();
        try {
            // 有就更新，没有则新增
            for (Productivity productivity : productivitys) {
                if (null == productivity.getPkid()) {
                    productivity.setAddedTime(new Timestamp(System.currentTimeMillis()));
                    productivityBiz.add(productivity);
                } else {
                    productivity.setModifyTime(new Timestamp(System.currentTimeMillis()));
                    productivityBiz.update(productivity);
                }
            }
            baseResponseData.setCode(ResponseCode.SUCCESS);
            baseResponseData.setMessage("添加成功！");
        } catch (Exception e) {
            logger.error("批量编辑异常！", e);
            baseResponseData.setCode(ResponseCode.DATA_EXCEPTION);
            baseResponseData.setMessage("批量编辑失败！");
        }
        return baseResponseData;
    }

}
